/*****************************************
 *  所属版本  企业号开发平台       *
 *****************************************/
package com.adds.lvds.service.impl.code;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.adds.lvds.core.trace.TraceContent;
import com.adds.lvds.core.trace.TraceService;
import com.adds.lvds.core.utils.ApplicationContextUtils;
import com.adds.lvds.dao.impl.code.SysCodeMapper;
import com.adds.lvds.model.code.SysCode;
import com.adds.lvds.model.code.SysCodeType;


/**
 * Description: 系统代码设置
 * @version $Revision: 1.1 $ 
 * @author zhangjian
 * @date 2017-12-07
 * @time: 8:53:06 PM
 */
public class SysCodeSetting {
	
	private static Map<String, SysCodeType> codeMap = 
		Collections.synchronizedMap(new HashMap<String, SysCodeType>());
	
	
	public synchronized static void load() {
		try {
			SysCodeMapper sysCodeMapper = (SysCodeMapper) ApplicationContextUtils.getBean("sysCodeMapper");
			List<SysCodeType> list = sysCodeMapper.findAllSysCodeType();
			for(SysCodeType codeType : list) {
				Map<String, SysCode> map = codeType.getCodeMap();
				List<SysCode> codes = codeType.getCodes();
				for(SysCode code : codes) {
					if(code.getCodeName() != null) {
						map.put(code.getCodeName(), code);
					}
				}
				if(codeType.getTypeName() != null) {
					codeMap.put(codeType.getTypeName(), codeType);
				}
			}
			TraceService.info(TraceContent.SYSTEM, 
					 "代码设置缓存初始化成功！");
		} catch(Exception e) {
			TraceService.fatal(TraceContent.SYSTEM, 
					 "代码设置缓存初始化失败！");
			e.printStackTrace();
		}
	}
	
	public synchronized static Map<String, SysCodeType> getCodeMap() {
		return codeMap;
	}

	public synchronized static SysCodeType getCodeType(String typeName) {
		if(typeName != null && !"".equals(typeName)) {
			return codeMap.get(typeName.trim().toUpperCase());
		}
		throw new java.lang.IllegalArgumentException("非法代码类型！");
	}
	
	public synchronized static SysCode getCode(String typeName, String codeName) {
		SysCodeType codeType = null;
		if(typeName != null && !"".equals(typeName)) {
			codeType = codeMap.get(typeName.trim().toUpperCase());
		} 
		
		if(codeType != null) {
			Map<String, SysCode> codes = codeType.getCodeMap();
			if(codeName != null && !"".equals(codeName)) {
				return codes.get(codeName.toUpperCase());
			} 
		}
		return null;
	}
	
	public synchronized static String getCodeValue(String typeName, String codeName) {
		SysCode code = getCode(typeName, codeName);
		if(code != null) {
			return code.getCodeValue().trim();
		}
		throw new java.lang.IllegalArgumentException("非法代码！");
	}
	
	public synchronized static String getCodeValue(String typeName, String codeName, String defaultValue) {
		SysCode code = getCode(typeName, codeName);
		if(code != null) {
			return code.getCodeValue().trim();
		}
		return defaultValue;
	}
	
	public synchronized static int getCodeIntValue(String typeName, String codeName) {
		try {
			return Integer.parseInt(getCodeValue(typeName, codeName));
		} catch(Exception e) {
			throw new java.lang.IllegalArgumentException("非法代码！", e);
		}
	}
	
	public synchronized static int getCodeIntValue(String typeName, String codeName, int defaultValue) {
		int v = defaultValue;
		try {
			v = Integer.parseInt(getCodeValue(typeName, codeName));
		} catch(Exception e) {
		}
		return v;
	}
	
	public synchronized static boolean getCodeBoolValue(String typeName, String codeName) {
		try {
			return Boolean.parseBoolean(getCodeValue(typeName, codeName));
		} catch(Exception e) {
			throw new java.lang.IllegalArgumentException("非法代码！", e);
		}
	}
	
	public synchronized static boolean getCodeBoolValue(String typeName, String codeName, boolean defaultValue) {
		boolean v = defaultValue;
		try {
			v = Boolean.parseBoolean(getCodeValue(typeName, codeName));
		} catch(Exception e) {
		}
		return v;
	}
	
	public synchronized static float getCodeFloatValue(String typeName, String codeName) {
		try {
			return Float.parseFloat(getCodeValue(typeName, codeName));
		} catch(Exception e) {
			throw new java.lang.IllegalArgumentException("非法代码！", e);
		}
	}
	
	public synchronized static float getCodeFloatValue(String typeName, String codeName, float defaultValue) {
		float v = defaultValue;
		try {
			v = Float.parseFloat(getCodeValue(typeName, codeName));
		} catch(Exception e) {
		}
		return v;
	}
	
	public synchronized static void setCode(String typeName, SysCode code) {
		SysCodeType codeType = codeMap.get(typeName);
		if(codeType != null) {
			Map<String, SysCode> map =  codeType.getCodeMap();
			if(map != null) {
				map.put(code.getCodeName(), code);
				TraceService.debug(TraceContent.SYSTEM, 
					 "[" + 
					typeName + "][" + code.getCodeName() + "]代码设置已改变！");
			}
		}
	}
	
	public synchronized static void removeCode(String typeName, String codeName) {
		SysCodeType codeType = codeMap.get(typeName);
		if(codeType != null) {
			Map<String, SysCode> map =  codeType.getCodeMap();
			if(map != null) {
				if(codeName != null) {
					map.remove(codeName);
				} else {
					map.clear();
				}
			}
		} else {
			codeMap.clear();
		}
	}
	
}
